import { Effect } from 'dva';
import { Reducer } from 'redux';
import { produce, Draft } from 'immer';

import {
  deviceType,
  longDeviceType,
  environmentDetail,
  environmentList,
  environmentStatistics,
  drugMedicalDetail,
  drugMedicalList,
  drugMedicalStatistics,
  byenergyConsumptionDetail,
  byenergyConsumptionList,
  byenergyConsumptionStatistics,
  vitalSignsDetail,
  vitalSignsList,
  vitalSignsStatistics,
  positionDetail,
  positionList,
  positionStatistics,
  goodsDetail,
  goodsList,
  goodsStatistics,
  sportsRecoveryDetail,
  sportsRecoveryList,
  sportsRecoveryStatistics,
  deviceStatistics,
  deviceList,
  deviceDetail,
  deviceStatisticsEx,
  deviceListEx,
  deviceKinds,
  drugListEx,
  infosDeviceCounts,
  treeTypeSelectAll,
} from '@/services/statistics';

//资产种类
export interface deviceKindsItem {
  deviceTypeNo?: number; //设备类型编号
  deviceTypeName?: string; //设备类型名称
}
// 设备类型父类查询
export interface DeviceTypeItem {
  deviceTypeName?: string; //设备父类名称
  deviceTypeNo?: number; //设备父类编号
}
// 统计约束
export interface Statistics {
  dayCount?: number; // 今日统计 ,
  everyCount?: number; // 所有统计 ,
  monthCount?: number; // 近一月 统计 ,
  weekCount?: number; // 近一周 统计
}

// 列表约束
export interface Item {
  deviceAlamCount: string; // 信息报警次数 ,
  deviceId: number; // 资产编号 ,
  deviceName: string; // 设备名称 ,
  deviceTypeName: string; // 设备资产类型 ,
  gatherCount: string; // 信息采集次数
}

// 环境信息
export interface EnvironmentDetailItem {
  collectTime: string; // 采集时间 ,
  deviceId: number; // 资产编号 ,
  illumination: string; // 照度(lx) ,
  pm: string; // PM2.5 ,
  pmTen: string; // PM10 ,
  positionDetails: string; // 安装区域 ,
  temperature: string; // 湿度 ,
  temperatureCelsius: string; // 温度(℃)
}

// 戒毒医疗
export interface DrugMedicalDetailItem {
  checkAge: number; // 人员年龄
  checkObject: string; // 监测对象
  checkSex: string; // 人员性别
  checkUserName: string; // 人员姓名
  collectTime: string; // 采集时间
  deviceId: number; // 资产编号
  operateDepart: string; // 操作人员部门
  operateName: string; // 操作人员姓名
  report: string; // 报告
}

// 能耗监测
export interface ByenergyDetailItem {
  creatTime: string; //上报时间;
  deviceId: number; //资产编号;
  treeTypeName: string; //安装区域;
}

// 生命体征
export interface VitalSignsDetailItem {
  breathing: string; // 呼吸(次/分钟) ,
  collecttime: string; // 采集时间 ,
  deviceId: number; //资产编号;
  diastolicPressure: string; // 舒张压(mmHg) ,
  heartRate: string; // 心率(次/分钟) ,
  moreOutBed: string; // 多人同时离床 ,
  outBed: string; // 离床(分钟) ,
  systolicePressure: string; // 收宿压(mmHg) ,
  temperature: string; // 体温(℃) ,
  userName: string; // 戒毒人员姓名
}

// 位置信息
export interface PositionDetailItem {
  createTime: string; // 上报时间 ,
  deviceId: number; //资产编号;
  positionCoordinates: string; // 位置坐标 ,
  wearingPersonnel: string; // 佩戴人员
}

// 物品信息
export interface GoodsDetailItem {
  collectTime: string; //采集时间;
  deviceId: number; //资产编号;
  goodsName: string; //物品名称;
  goodsNo: string; //物品编号;
  goodsState: string; //物品状态;
  goodsType: string; //物品种类;
  position: string; //所在位置;
}

// 运动康复
export interface SportsRecoveryDetailItem {
  collectTime: string; //采集时间;
  deviceId: number; //资产编号;
  report: string; //测试报告;
  testTime: string; //测试时间;
  userName: string; //测试人员姓名;
}
// 设备统计查询约束
export interface DeviceStatisticsItem {
  deviceAlam: number; //设备报警次数;
  deviceCount: number; //设备总数;
  deviceFault: number; //设备故障数;
  deviceOffLine: number; //设备离线;
  deviceOnLine: number; //设备在线 ;
  deviceSonTypeNo: string; //资产种类编号;
  deviceTypeNo: string; //资产类别编号;
  typeName: string; //资产类别;
  typeSonName: string; //资产种类;
}
export interface deviceDetailItem {
  deviceAlam: string; //设备报警次数 ,
  deviceCount: number; //设备总数 ,
  deviceId: string; //设备编号 ,
  deviceName: string; //设备名称 ,
  deviceSonTypeNo: string; //资产种类编号 ,
  deviceState: number; //设备状态 0在线，1离线 ,
  deviceTypeNo: string; //资产类别编号 ,
  stateCode: number; //在线状态 0正常，1异常 ,
  typeName: string; //资产种类 ,
  typeSonName: string; //资产类别
}
// 详情采集分析数据约束
export interface commListDetail {
  deviceId: number;
  deviceName: string;
  deviceTypeName: string;
}
//设备统计约束
export interface DeviceStatistics {
  deviceCount: number; // 设备总数;
  deviceFaultCount: number; // 设备故障数 ,
  deviceNormalCount: number; // 设备正常数;
}
// 设备统计状态
export interface DeviceVisible {
  visible: number;
  visibleStatus: String;
  deviceState: String;
  deviceSonTypeNo: String;
  deviceTypeParentNo: String;
}
export interface defaultTabsActiveKey {
  defaultActiveKey: string;
}
export interface StatisticsModelState {
  statistics: Statistics;
  byeStatistics: Statistics;
  deviceType: DeviceTypeItem[];
  longDeviceType: DeviceTypeItem[];
  environmentDetails: EnvironmentDetailItem[]; // 环境信息详情
  environmentList: Item[]; // 环境信息列表
  drugMedicalDetails: DrugMedicalDetailItem[]; // 戒毒医疗详情
  drugMedicalList: Item[]; // 戒毒医疗列表
  byenergyDetails: ByenergyDetailItem[]; // 能耗监测详情
  byenergyList: Item[]; // 能耗监测列表
  vitalSignsDetails: VitalSignsDetailItem[]; // 生命体征详情
  vitalSignsList: Item[]; // 生命体征列表
  positionDetails: PositionDetailItem[]; // 位置信息详情
  positionList: Item[]; // 位置信息列表
  goodsDetails: GoodsDetailItem[]; // 物品信息详情
  goodsList: Item[]; // 物品信息列表
  sportsRecoveryDetails: SportsRecoveryDetailItem[]; // 运动康复详情
  sportsRecoveryList: Item[]; // 运动康复列表

  deviceKinds: deviceKindsItem[]; //资产种类
  deviceList: DeviceStatisticsItem[]; //设备统计列表查询
  deviceStatistics: DeviceStatistics; //设备统计查询
  deviceDetail: deviceDetailItem[]; //设备明细详情查询
  commListDetail: commListDetail; //详情采集分析数据约束
  deviceVisible: DeviceVisible;
  defaultTabsActiveKey: defaultTabsActiveKey;
}
export interface StatisticsModelType {
  namespace: string;
  state: StatisticsModelState;
  effects: {
    deviceKinds: Effect;
    deviceType: Effect;
    longDeviceType: Effect;
    environmentDetail: Effect;
    environmentList: Effect;
    environmentStatistics: Effect;
    drugMedicalDetail: Effect;
    drugMedicalList: Effect;
    drugMedicalStatistics: Effect;
    drugListEx: Effect;
    byenergyConsumptionDetail: Effect;
    byenergyConsumptionList: Effect;
    byenergyConsumptionStatistics: Effect;
    vitalSignsDetail: Effect;
    vitalSignsList: Effect;
    vitalSignsStatistics: Effect;
    positionDetail: Effect;
    positionList: Effect;
    // positionStatistics: Effect;
    goodsDetail: Effect;
    goodsList: Effect;
    goodsStatistics: Effect;
    sportsRecoveryDetail: Effect;
    sportsRecoveryList: Effect;
    sportsRecoveryStatistics: Effect;
    deviceStatistics: Effect;
    deviceList: Effect;
    deviceDetail: Effect;
    // deviceStatisticsEx: Effect;
    // deviceListEx: Effect;
    commListDetail: Effect;
    deviceVisible: Effect;
    defaultTabsActiveKey: Effect;
    treeTypeSelectAll: Effect;
    infosDeviceCounts: Effect;
    deviceDetailReturn: Effect;
  };
  reducers: {
    change_environmentDetail: Reducer<StatisticsModelState>;
    change_environmentList: Reducer<StatisticsModelState>;
    change_statistics: Reducer<StatisticsModelState>;
    change_drugMedicalDetail: Reducer<StatisticsModelState>;
    change_drugMedicalList: Reducer<StatisticsModelState>;
    change_sportsRecoveryDetail: Reducer<StatisticsModelState>;
    change_sportsRecoveryList: Reducer<StatisticsModelState>;
    change_longDeviceType: Reducer<StatisticsModelState>;
    change_byeStatistics: Reducer<StatisticsModelState>;
    change_positionList: Reducer<StatisticsModelState>;
    change_positionDetail: Reducer<StatisticsModelState>;
    change_treeTypeSelectAll:Reducer<StatisticsModelState>;
    change_infosDeviceCounts:Reducer<StatisticsModelState>;
    change_deviceDetailReturn:Reducer<StatisticsModelState>;
  };
}

const Model: StatisticsModelType = {
  namespace: 'statistics',

  state: {
    deviceKinds: [],
    deviceType: [],
    longDeviceType: [],
    statistics: {},
    environmentDetails: [],
    environmentList: [],
    drugMedicalDetails: [],
    drugMedicalList: [],
    byenergyDetails: [],
    byenergyList: [],
    vitalSignsDetails: [],
    vitalSignsList: [],
    positionDetails: [],
    positionList: [],
    goodsDetails: [],
    goodsList: [],
    sportsRecoveryDetails: [],
    sportsRecoveryList: [],
    deviceStatistics: {},
    byeStatistics: {},
    deviceList: [],
    deviceDetail: [],
    commListDetail: {},
    deviceVisible: {
      visible: 0,
      visibleStatus: '1',
      deviceState: '',
      deviceSonTypeNo: '',
      deviceTypeParentNo: '',
    },
    defaultTabsActiveKey: {
      defaultActiveKey: '',
    },
    treeTypeData: [],
    infosCountsData: [],
    returnData: 'true',
  },

  effects: {
    *deviceKinds({ payload }, { call, put }) {
      const [err, data] = yield call(deviceKinds, payload);
      if (!err) yield put({ type: 'change_deviceKinds', payload: data });
    },
    *deviceType({ payload }, { call, put }) {
      const [err, data] = yield call(deviceType, payload);
      if (!err) yield put({ type: 'change_deviceType', payload: data });
    },
    *longDeviceType({ payload }, { call, put }) {
      const [err, data] = yield call(longDeviceType, payload);
      if (!err) yield put({ type: 'change_longDeviceType', payload: data });
    },
    *environmentDetail({ payload }, { call, put }) {
      const [err, data] = yield call(environmentDetail, payload);
      if (!err) yield put({ type: 'change_environmentDetail', payload: data });
    },
    *environmentList({ payload }, { call, put }) {
      const [err, data] = yield call(environmentList, payload);
      if (!err) yield put({ type: 'change_environmentList', payload: data });
    },
    *environmentStatistics({ payload }, { call, put }) {
      const [err, data] = yield call(environmentStatistics, payload);
      if (!err) yield put({ type: 'change_statistics', payload: data });
    },
    *byenergyConsumptionDetail({ payload }, { call, put }) {
      const [err, data] = yield call(byenergyConsumptionDetail, payload);
      if (!err) yield put({ type: 'change_byenergyConsumptionDetail', payload: data });
    },
    *byenergyConsumptionList({ payload }, { call, put }) {
      const [err, data] = yield call(byenergyConsumptionList, payload);
      if (!err) yield put({ type: 'change_byenergyConsumptionList', payload: data });
    },
    *byenergyConsumptionStatistics({ payload }, { call, put }) {
      // 能耗监测
      const [err, data] = yield call(byenergyConsumptionStatistics, payload);
      if (!err) yield put({ type: 'change_byeStatistics', payload: data });
    },
    *vitalSignsDetail({ payload }, { call, put }) {
      const [err, data] = yield call(vitalSignsDetail, payload);
      if (!err) yield put({ type: 'change_vitalSignsDetail', payload: data });
    },
    *vitalSignsList({ payload }, { call, put }) {
      const [err, data] = yield call(vitalSignsList, payload);
      if (!err) yield put({ type: 'change_vitalSignsList', payload: data });
    },
    *vitalSignsStatistics({ payload }, { call, put }) {
      const [err, data] = yield call(vitalSignsStatistics, payload);
      if (!err) yield put({ type: 'change_statistics', payload: data });
    },
    *drugMedicalDetail({ payload }, { call, put }) {
      const [err, data] = yield call(drugMedicalDetail, payload);
      if (!err) yield put({ type: 'change_drugMedicalDetail', payload: data });
    },
    *drugMedicalList({ payload }, { call, put }) {
      const [err, data] = yield call(drugMedicalList, payload);
      if (!err) yield put({ type: 'change_drugMedicalList', payload: data });
    },
    *drugMedicalStatistics({ payload }, { call, put }) {
      const [err, data] = yield call(drugMedicalStatistics, payload);
      if (!err) yield put({ type: 'change_statistics', payload: data });
    },
    *drugListEx({ payload }, { call, put }) {
      //导出
      const url = yield call(payload.typeApi, payload.params);
      // const blob = new Blob([url], {
      //   type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8',
      // });
      const downloadElement = document.createElement('a');
      const href = window.URL.createObjectURL(url); // 创建下载的链接
      downloadElement.href = href;
      downloadElement.download = payload.name; // 下载后文件名
      document.body.appendChild(downloadElement);
      downloadElement.click(); // 点击下载
      document.body.removeChild(downloadElement); // 下载完成移除元素
      window.URL.revokeObjectURL(href); // 释放掉blob对象
    },
    *sportsRecoveryDetail({ payload }, { call, put }) {
      const [err, data] = yield call(sportsRecoveryDetail, payload);
      if (!err) yield put({ type: 'change_sportsRecoveryDetail', payload: data });
    },
    *sportsRecoveryList({ payload }, { call, put }) {
      const [err, data] = yield call(sportsRecoveryList, payload);
      if (!err) yield put({ type: 'change_sportsRecoveryList', payload: data });
    },
    *sportsRecoveryStatistics({ payload }, { call, put }) {
      const [err, data] = yield call(sportsRecoveryStatistics, payload);
      if (!err) yield put({ type: 'change_statistics', payload: data });
    },
    *goodsDetail({ payload }, { call, put }) {
      const [err, data] = yield call(goodsDetail, payload);
      if (!err) yield put({ type: 'change_goodsDetail', payload: data });
    },
    *goodsList({ payload }, { call, put }) {
      const [err, data] = yield call(goodsList, payload);
      if (!err) yield put({ type: 'change_goodsList', payload: data });
    },
    *goodsStatistics({ payload }, { call, put }) {
      const [err, data] = yield call(goodsStatistics, payload);
      if (!err) yield put({ type: 'change_statistics', payload: data });
    },
    *positionList({ payload }, { call, put }) {
      const [err, data] = yield call(positionList, payload);
      if (!err) yield put({ type: 'change_positionList', payload: data });
    },
    *positionDetail({ payload }, { call, put }) {
      const [err, data] = yield call(positionDetail, payload);
      if (!err) yield put({ type: 'change_positionDetail', payload: data });
    },
    *deviceStatistics({ payload }, { call, put }) {
      const [err, data] = yield call(deviceStatistics, payload);
      if (!err) yield put({ type: 'change_deviceStatistics', payload: data });
    },
    *deviceDetail({ payload }, { call, put }) {
      const [err, data] = yield call(deviceDetail, payload);
      if (!err) yield put({ type: 'change_deviceDetail', payload: data });
    },
    *deviceList({ payload }, { call, put }) {
      const [err, data] = yield call(deviceList, payload);
      if (!err) yield put({ type: 'change_deviceList', payload: data });
    },
    *commListDetail({ payload }, { put }) {
      yield put({ type: 'change_commListDetail', payload: payload });
    },
    *deviceVisible({ payload }, { put }) {
      yield put({ type: 'change_deviceVisible', payload: payload });
    },
    *infosDeviceCounts({ payload }, { call,put }) {
      const [err, data] = yield call(infosDeviceCounts, payload);
      if (!err) yield put({ type: 'change_infosDeviceCounts', payload: data });
    },
    *treeTypeSelectAll({ payload }, { call, put }) {
      const [err, data] = yield call(treeTypeSelectAll, payload);
      if (!err) yield put({ type: 'change_treeTypeSelectAll', payload: data });
    },
    *defaultTabsActiveKey({ payload }, { put }) {
      yield put({ type: 'change_defaultTabsActiveKey', payload: payload });
    },
    *deviceDetailReturn({ payload }, { put }) {
      console.log('45456465465456456456');
      yield put({ type: 'change_deviceDetailReturn', payload: payload });
    },
  },

  reducers: {
    userList: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.userList = payload;
    }),
    change_deviceKinds: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.deviceKinds = payload;
    }),
    change_deviceType: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.deviceType = payload;
    }),
    change_longDeviceType: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.longDeviceType = payload;
    }),
    change_drugMedicalDetail: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      payload.rows.forEach((element,index) => {
        payload.rows[index].checkSex = element.checkSex === '0' ? '男' : '女';
      });
      draft.drugMedicalDetails = payload;
    }),
    change_drugMedicalList: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.drugMedicalList = payload;
    }),
    change_environmentDetail: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.environmentDetails = payload;
    }),
    change_environmentList: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.environmentList = payload;
    }),
    change_vitalSignsDetail: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.vitalSignsDetails = payload;
    }),
    change_vitalSignsList: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.vitalSignsList = payload;
    }),
    change_byenergyConsumptionDetail: produce(
      (draft: Draft<StatisticsModelState>, { payload }): void => {
        draft.byenergyDetails = payload;
      },
    ),
    change_byenergyConsumptionList: produce(
      (draft: Draft<StatisticsModelState>, { payload }): void => {
        draft.byenergyList = payload;
      },
    ),
    change_byeStatistics: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.byeStatistics = payload;
    }),
    change_statistics: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.statistics = payload;
    }),
    change_sportsRecoveryDetail: produce(
      (draft: Draft<StatisticsModelState>, { payload }): void => {
        draft.sportsRecoveryDetail = payload;
      },
    ),
    change_sportsRecoveryList: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.sportsRecoveryList = payload;
    }),
    change_goodsDetail: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.goodsDetails = payload;
    }),
    change_goodsList: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.goodsList = payload;
    }),
    change_positionDetail: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.positionDetail = payload;
    }),
    change_positionList: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      console.log(payload);
      draft.positionList = payload;
    }),
    change_deviceStatistics: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.deviceStatistics = payload;
    }),
    change_deviceDetail: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.deviceDetail = payload;
    }),
    change_deviceList: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.deviceList = payload;
    }),
    change_commListDetail: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.commListDetail = payload;
    }),
    change_deviceVisible: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.deviceVisible = payload;
    }),
    change_treeTypeSelectAll: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      draft.treeTypeData = payload;
    }),
    change_defaultTabsActiveKey: produce(
      (draft: Draft<StatisticsModelState>, { payload }): void => {
        draft.defaultTabsActiveKey = payload;
      },
    ),
    change_infosDeviceCounts: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      console.log(payload);
      draft.infosCountsData = payload;
    }),
    change_deviceDetailReturn: produce((draft: Draft<StatisticsModelState>, { payload }): void => {
      console.log(payload);
      console.log('周五ul');
      draft.returnData = payload;
      console.log(draft.returnData);
    }),
  },
};

export default Model;
